<template>
    <div class="box">
        <headers title="注册"></headers>
        <img src="../../assets/login1.png" alt="" srcset="" style="width: 100%;">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                    :rules="[{ pattern: pattern1, required: true, message: '请填写用户名' }]" />
                <div style="display: flex; align-items: center;">
                    <van-field v-model="password" name="password" label="密码" placeholder="密码"
                        :rules="[{ pattern: pattern2, required: true, message: '请填写密码' }]" style="width: 90%;"
                        v-if="isShow == true">
                    </van-field>
                    <van-field v-model="password" name="password" type="password" label="密码" placeholder="密码"
                        :rules="[{ pattern: pattern2, required: true, message: '请填写密码' }]" style="width: 90%;" v-else>
                    </van-field>
                    <van-switch v-model="checked" @click="codeFun" />
                </div>
            </van-cell-group>
            <p style="margin-left: 28rem; color: blue;margin-top: 2rem;">忘记密码</p>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    注册
                </van-button>
            </div>
        </van-form>
        <router-link to="/login">
            <p style="display: flex;align-items: center;justify-content: center; color: blue;">注册</p>
        </router-link>
        <img src="../../assets/login2.png" alt="" srcset="" style="width: 100%;">
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { registerApi } from '@/api/api';
import { showToast } from 'vant';
import headers from '@/components/headers.vue';
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const route = useRoute()
import { useCounterStore } from '@/stores/counter'
// 在组件内部的任何地方均可以访问变量 `store` ✨
const store = useCounterStore()
const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    registerApi(username.value, password.value).then((res) => {
        if (res.data.code == 0) {
            showToast("注册成功")
            router.push('/login')
        } else {
            showToast(res.data.msg)
        }
    })
};

const checked = ref(false);
const pattern1 = /^\d{11,11}$/;
const pattern2 = /^\w{8,}$/;
const isShow = ref(true)
const codeFun = () => {
    checked.value != checked.value
    isShow.value = false
}
</script>

<style lang="scss" scoped>
.box {
    background: #f9f9f9;
}
</style>